package com.cennavi.bigdata.baotou.util;

import java.io.*;
import java.util.*;

public class ReadTxt {

	 /**
     * 读取某个文件夹下的所有文件
     */
	public static Map<String,Object> readfile(String filepath) throws FileNotFoundException, IOException {
    	Map<String,Object> readMap = new HashMap<String,Object>();
            try {
                File weatherFile = new File(filepath);
                String[] split = weatherFile.list();
                List<Long> list = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    list.add(Long.valueOf(split[i]));
                }
                File file = new File(filepath + Collections.max(list));
                if (file.isFile()) {
//                    	System.out.println("文件");
                		if (file.getName().contains(".txt")) {
                			readMap.putAll(readTxtFile(file));
                		}else{
//                			System.out.println("不是txt文件");
                		}
                    } else if (file.isDirectory()) {
//                            System.out.println("文件夹");
                            String[] filelist = file.list();
                        List<Long> longs = new ArrayList<>();
                        for (int i = 0; i < filelist.length; i++) {
                            long filetime = Long.valueOf(filelist[i].split("\\.")[0]);
                            longs.add(filetime);
                        }
//                        System.out.println(Collections.max(longs).toString());
                            File readfile = new File(filepath + Collections.max(list) + File.separator + Collections.max(longs) + ".txt");
//                                    if (!readfile.isDirectory()) {
//                            System.out.println(filepath + Collections.max(list) + File.separator + Collections.max(longs) + ".txt");
                            if (readfile.isFile()) {
                                if (readfile.getName().contains(".txt")) {
                                    readMap.putAll(readTxtFile(readfile));
                                }else{
//                                    System.out.println("不是txt文件");
                                }
                            } else if (readfile.isDirectory()) {
                                readfile(filepath + Collections.max(list) + File.separator + Collections.max(longs) + ".txt");
                            }
                            /*for (int i = 0; i < filelist.length; i++) {
                                    File readfile = new File(filepath + File.separator + filelist[i]);
//                                    if (!readfile.isDirectory()) {
                                    System.out.println(readfile.getPath());
                                    if (readfile.isFile()) {
                                		if (readfile.getName().contains(".txt")) {
                                			readMap.putAll(readTxtFile(readfile));
                                		}else{
                                			System.out.println("不是txt文件");
                                		}
                                    } else if (readfile.isDirectory()) {
                                            readfile(filepath + File.separator + filelist[i]);
                                    }
                            }*/

                    }

            } catch (FileNotFoundException e) {
                    System.out.println("readfile()   Exception:" + e.getMessage());
            }
            return readMap;
    }

    /**
     * 功能：Java读取txt文件的内容
     * 步骤：1：先获得文件句柄
     * 2：获得文件句柄当做是输入一个字节码流，需要对这个输入流进行读取
     * 3：读取到输入流后，需要读取生成字节流
     * 4：一行一行的输出。readline()。
     * 备注：需要考虑的是异常情况
     * @return  Map<String,String>
     */
    public static Map<String,Object> readTxtFile(File file){
    	Map<String,Object> readMap = new HashMap<String,Object>();
        try {
                String encoding="UTF-8";
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    Set<String > set = new TreeSet<>();
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                        System.out.println(lineTxt);
                        String[] txtSplit = lineTxt.split(",");
                        readMap.put(txtSplit[0]+txtSplit[1]+txtSplit[2]+txtSplit[4]+txtSplit[5],lineTxt);
                        set.add(txtSplit[6].substring(0, txtSplit[6].length() - 2));
                    }
                    readMap.put("dateset",set);
                    if(bufferedReader!=null){
                        bufferedReader.close();
                    }
                    if(read!=null){
                        read.close();
                    }
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return readMap;
    }
    
    /**
     * 删除某个文件夹下的所有文件夹和文件
     */
    
    
    /*public static boolean deletefile(String delpath)
                    throws FileNotFoundException, IOException {
            try {

                    File file = new File(delpath);
                    if (!file.isDirectory()) {
                            System.out.println("1");
                            file.delete();
                    } else if (file.isDirectory()) {
                            System.out.println("2");
                            String[] filelist = file.list();
                            for (int i = 0; i < filelist.length; i++) {
                                    File delfile = new File(delpath + "\\" + filelist[i]);
                                    if (!delfile.isDirectory()) {
                                            System.out.println("path=" + delfile.getPath());
                                            System.out.println("absolutepath="
                                                            + delfile.getAbsolutePath());
                                            System.out.println("name=" + delfile.getName());
                                            delfile.delete();
                                            System.out.println("删除文件成功");
                                    } else if (delfile.isDirectory()) {
                                            deletefile(delpath + "\\" + filelist[i]);
                                    }
                            }
                            file.delete();

                    }

            } catch (FileNotFoundException e) {
                    System.out.println("deletefile()   Exception:" + e.getMessage());
            }
            return true;
    }*/
    
   /* public static void main(String[] args) {
            try {
            	Map<String,String> readMaps =readfile(BaotouInformation.getFilepath());
            	System.out.println(readMaps.toString());
                    // deletefile("D:/file");
            } catch (FileNotFoundException ex) {
            } catch (IOException ex) {
            }
            System.out.println("ok");
    }*/
}
